package com.wokeMy.view.Lottery;

import android.animation.ObjectAnimator;
import android.app.Dialog;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.wokeMy.view.adpter.RecordAdapter;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.model.Ticket;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class RecordBetActivity extends BaseActivity implements View.OnClickListener{
    TextView allTv,winTv,waiTv,failTv;
    View red_line;
    ListView record_betting_lv;
    ArrayList<String> list=new ArrayList<>();
    ImageView record_bet_back;
    int width;
    String user_id;
    Dialog mydialog;
    ArrayList<Ticket> tickets;
    RecordAdapter recordAdapter;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_record_bet);
    }

    @Override
    public void initActivity() {
        recordAdapter = new RecordAdapter(getApplicationContext(),tickets);
        record_betting_lv.setAdapter(recordAdapter);
        allTv.setOnClickListener(this);
        winTv.setOnClickListener(this);
        waiTv.setOnClickListener(this);
        failTv.setOnClickListener(this);
        record_bet_back.setOnClickListener(this);
        mydialog=Util.createLoadingDialog(RecordBetActivity.this);
        myTicket("");
        record_betting_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (tickets.get(position).getErrormsg()==null){
                    tishi("该订单提交失败");
                }else {
                    //出票状态 0不可出票 1可出票状态 2出票成功 3出票失败(允许再出票）4出票中 5出票中（体彩中心）6出票失败（不允许出票） 99 为空
                    if (tickets.get(position).getTicketstatus().equals("99")||tickets.get(position).getTicketstatus().equals("4")||tickets.get(position).getTicketstatus().equals("5")) {
                        Intent intent=new Intent(RecordBetActivity.this,DetailBetActivity.class);
                        intent.putExtra("id",tickets.get(position).getId());
                        startActivity(intent);

                    }else {
                        if (tickets.get(position).getTicketstatus().equals("2")||tickets.get(position).getTicketstatus().equals("1")){
                            //2出票成功
                            switch (tickets.get(position).getZjstatus()) {
                                case "":
                                case "0":
                                case "1":
                                case "2":
                                case "5":
                                    Intent intent=new Intent(RecordBetActivity.this,DetailBetActivity.class);
                                    intent.putExtra("id",tickets.get(position).getId());
                                    startActivity(intent);
                                    break;
                                default:
                                    tishi("该订单提交失败");
                                    break;
                            }
                        }else {
                            tishi("该订单提交失败");
                        }
                    }
                }
            }
        });
    }

    @Override
    public void initView() {
        record_bet_back=myFindViewById(R.id.record_bet_back);
        allTv=myFindViewById(R.id.all_record_tv);
        winTv=myFindViewById(R.id.win_record_tv);
        waiTv=myFindViewById(R.id.wait_record_tv);
        failTv=myFindViewById(R.id.fail_record_tv);
        red_line=myFindViewById(R.id.red_line);
        record_betting_lv=myFindViewById(R.id.record_betting_lv);
    }

    @Override
    public void initData() {
        user_id=((MyApp)getApplication()).getDatas_load().getId();
        tickets=new ArrayList<>();
        for (int i=0;i<4;i++){
            list.add("TAB "+i);
        }
        width = getWindow().getWindowManager().getDefaultDisplay().getWidth()/4;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.all_record_tv:
                mydialog=Util.createLoadingDialog(RecordBetActivity.this);
                myTicket("");
                // 启动动画
                ObjectAnimator//
                        .ofFloat(red_line, "translationX", 0)//
                        .setDuration(500)//
                        .start();
                allTv.setTextColor(getResources().getColor(R.color.redtext));
                winTv.setTextColor(getResources().getColor(R.color.date_graly));
                waiTv.setTextColor(getResources().getColor(R.color.date_graly));
                failTv.setTextColor(getResources().getColor(R.color.date_graly));
                break;

            case R.id.win_record_tv:
                mydialog=Util.createLoadingDialog(RecordBetActivity.this);
                myTicket("2");
                ObjectAnimator//
                        .ofFloat(red_line, "translationX", width)//
                        .setDuration(500)//
                        .start();
                allTv.setTextColor(getResources().getColor(R.color.date_graly));
                winTv.setTextColor(getResources().getColor(R.color.redtext));
                waiTv.setTextColor(getResources().getColor(R.color.date_graly));
                failTv.setTextColor(getResources().getColor(R.color.date_graly));
                break;

            case R.id.wait_record_tv:
                mydialog=Util.createLoadingDialog(RecordBetActivity.this);
                myTicket("0");
                ObjectAnimator//
                        .ofFloat(red_line, "translationX", width*2)//
                        .setDuration(500)//
                        .start();
                allTv.setTextColor(getResources().getColor(R.color.date_graly));
                winTv.setTextColor(getResources().getColor(R.color.date_graly));
                waiTv.setTextColor(getResources().getColor(R.color.redtext));
                failTv.setTextColor(getResources().getColor(R.color.date_graly));
                break;
            case R.id.fail_record_tv:
                mydialog=Util.createLoadingDialog(RecordBetActivity.this);
                myTicket("1");
                ObjectAnimator//
                        .ofFloat(red_line, "translationX", width*3)//
                        .setDuration(500)//
                        .start();
                allTv.setTextColor(getResources().getColor(R.color.date_graly));
                winTv.setTextColor(getResources().getColor(R.color.date_graly));
                waiTv.setTextColor(getResources().getColor(R.color.date_graly));
                failTv.setTextColor(getResources().getColor(R.color.redtext));
                break;
            case R.id.record_bet_back:
                finish();
                break;

            default:
                break;
        }
    }
    // 我的彩票
    public void  myTicket(String type){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("page_no","1");//页码
        params.put("type",type);// 中奖状态 0： 未开奖 1 未中奖 2 中奖 5 可算奖状态 空为全部
        Log.e("我的彩票params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.CPMY_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                closeLoadDial(mydialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("我的彩票result", result);
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        tickets.clear();
                        if (jsonObject.get("flag").equals("success")) {
                            if (jsonObject.has("data")){
                                Gson gson = new Gson();
                                JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("data");
                                for (int i1 = 0; i1 < jsonArray.length(); i1++) {
                                    Ticket ticket = gson.fromJson(jsonArray.getString(i1), Ticket.class);
                                    tickets.add(ticket);
                                }
                            }
                            recordAdapter.notifyDataSetChanged();
                        } else {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                closeLoadDial(mydialog);
            }
        });
    }

}
